//package com.lrd.snapshotorder.service.orderconsumer;
//
//import com.lrd.snapshotorder.config.redis.RedissonClientService;
//import com.lrd.snapshotorder.entity.SnapshotOrder;
//import com.lrd.snapshotorder.mapper.SnapshotOrderMapper;
//import org.redisson.api.RBlockingDeque;
//import org.springframework.beans.factory.InitializingBean;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.stereotype.Component;
//
//import java.util.concurrent.ThreadPoolExecutor;
//
//@Component
//public class OrderGeneratorConsumer implements InitializingBean {
//
//    @Autowired
//    @Qualifier(value = "orderConsumerThreadPool")
//    private ThreadPoolExecutor orderConsumerThreadPool;
//    @Autowired
//    private SnapshotOrderMapper snapshotOrderMapper;
//    @Autowired
//    private RedissonClientService redissonClientService;
//
//
//    @Override
//    public void afterPropertiesSet() throws Exception {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                RBlockingDeque rBlockingDeque = redissonClientService.getRBlockingDeque();
//                while (true) {
//                    SnapshotOrder order = (SnapshotOrder) rBlockingDeque.poll();
//                    if (order != null) {
//                        orderConsumerThreadPool.execute(() -> {
//                            snapshotOrderMapper.insert(order);
//                        });
//                    }
//                }
//            }
//                /*try {
//                    List<SnapshotOrder> batch = new ArrayList<>();
//                    while (true) {
//                        SnapshotOrder order = (SnapshotOrder) rBlockingDeque.poll();
//                        if (order == null) {
//                            if (CollUtil.isNotEmpty(batch)) {
//                                snapshotOrderMapper.insertBatch(batch);
//                                batch.clear();
//                            }
//                            order = (SnapshotOrder) rBlockingDeque.take();
//                        }
//                        batch.add(order);
//                        if (batch.size() >= 500) {
//                            snapshotOrderMapper.insertBatch(batch);
//                            batch.clear();
//                        }
//                    }
//                } catch (Exception e) {
//                    throw new RuntimeException(e);
//                }
//            }*/
//        }).start();
//
//    }
//}
